////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2009 Julien Etienne.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
package org.siam.impl {
	
	import flash.utils.Dictionary;
	
	import org.siam.core.ISiamManager;
	import org.siam.core.data.editors.IEditorsManager;
	import org.siam.core.data.formatters.IFormattersManager;
	import org.siam.core.data.renderers.IRenderersManager;
	import org.siam.core.data.validators.IValidatorsManager;
	import org.siam.core.schemas.ISchema;
	import org.siam.core.schemas.ISchemaManager;
	import org.siam.impl.schemas.Schema;
	import org.siam.impl.schemas.SchemaManager;
	
	
	[DefaultProperty(name="configuration")]
	public class SiamManager implements ISiamManager {
		
		internal var _schemas:Dictionary;
		
		// managers
		internal var _formatters:IFormattersManager;
		internal var _renderers:IRenderersManager;
		internal var _editors:IEditorsManager;
		internal var _validators:IValidatorsManager;
		
		public function SiamManager() {
			_schemas = new Dictionary();
		}

		[Inspectable(category="General")]
		public function set configuration(value:XML):void {
			SiamManagerXMLHandler.read(value, this);
		}
		
		public function get formatters():IFormattersManager {
			return _formatters;			
		}
		
		public function get renderers():IRenderersManager {
			return _renderers;			
		}
		
		public function get editors():IEditorsManager {
			return _editors;			
		}
		
		public function get validators():IValidatorsManager {
			return _validators;			
		}
		
		// Import setters are obsolete, they help to structure the import and not forget them
		[ArrayElementType("Class")]
		public function set importFormatters(value:Array):void { }
		
		[ArrayElementType("Class")]
		public function set importRenderers(value:Array):void { }
		
		[ArrayElementType("Class")]
		public function set importEditors(value:Array):void { }
		
		[ArrayElementType("Class")]
		public function set importValidators(value:Array):void { }
		
		public function manage(schemaId:String):ISchemaManager {
			var schema:ISchema = findSchema(schemaId);
			return new SchemaManager(this, schema);
		}
		
		public function get schemaIds():Array {
			var ids:Array = new Array();
			for (var id:String in _schemas) {
				ids.push(id);
			}
			return ids;
		}
		
		internal function hasSchema(id:String):Boolean {
			return _schemas.hasOwnProperty(id);
		}
		
		internal function findSchema(id:String):ISchema {
			return hasSchema(id) ? _schemas[id] : Schema.NULL;
		}
		
		internal function registerSchema(id:String, schema:ISchema):void {
			if (!hasSchema(id)) {
				_schemas[id] = schema;
			}
		}
	}
}